# 类
class student:
    def __init__(self, name, age):
        # self 是类执行的结果
        self.name = name
        self.age = age


# 对类进行实例化(对类进行调用)
# 类进行调用 必定有返回值 这个返回值我们称为对象(实例)
damu = student("damu",18)
# {"name":"damu","age":18}
# print(damu.name)
# print(damu.age)

# 1. 链表（linked list）是一种线性数据结构
# 2. 链表是由节点对象组成的,各个节点通过“引用”相连接
# 3. 引用记录了下一个节点的内存地址

# 初始化列表
class ListNode:
    """链表节点类"""
    def __init__(self, val: int):
        self.val: int = val               # 节点值
        self.next: ListNode | None = None # 指向下一节点的引用

n0 = ListNode(1)
n1 = ListNode(3)
n2 = ListNode(2)
n3 = ListNode(5)
n4 = ListNode(4)

n0.next = n1
n1.next = n2
n2.next = n3
n3.next = n4

# 链表的插入
def insert(n0: ListNode, P: ListNode):
    """在链表的节点 n0 之后插入节点 P"""
    n1 = n0.next #找到n0之后的节点n1
    P.next = n1  #将p的next指向n1
    n0.next = P  #将n0的next断开指向p

# 链表的删除
def remove(n0: ListNode):
    """删除链表的节点 n0 之后的首个节点"""
    if not n0.next:
        return
    P = n0.next
    n1 = P.next
    n0.next = n1

# 访问节点 根据index找对应的节点
def access(head: ListNode, index: int) -> ListNode | None:
    """访问链表中索引为 index 的节点"""
    for _ in range(index):
        if not head:
            return None
        head = head.next
    return head

# print(access(n0, 3).val)

def find(head: ListNode, target: int) -> int:
    """在链表中查找值为 target 的首个节点"""
    index = 0
    while head:
        if head.val == target:
            return index
        head = head.next
        index += 1
    return -1

print(find(n0, 50))